package com.sihai.ehr.web.action.exam;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.ExamStatistics;
import com.sihai.ehr.model.sys.ExaminePeriodEnum;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.service.ExamAppManageService;
import com.sihai.ehr.service.ExamPeriodService;
import com.sihai.ehr.service.ExamStatisticsService;
import com.sihai.ehr.service.OrganizationService;
import com.sihai.ehr.web.action.EHRAction;
import com.sihai.ehr.web.pojo.ExamFacetalkPojo;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

/**
 * User: zj
 * Date: 2012-10-24
 * Time: 20:19:21
 */
@Results({
        @Result(name = "main", location = "exam_facetalk_main.jsp"),
        @Result(name = "list", location = "exam_facetalkorg_list.jsp")
})
public class ExamFaceTalkAction extends EHRAction {

    @BeanInject
    private OrganizationService organizationService;
    @BeanInject
    private ExamAppManageService examAppManageService;
    @BeanInject
    private ExamPeriodService examPeriodService;
    @BeanInject
    private ExamStatisticsService examStatisticsService;

    private List<Organization> organizations;
    private LimitedList<ExamFacetalkPojo> examFacetalkPojos;
    /**
     * 考核周期类型
     */
    private ExaminePeriodEnum type;
    /**
     * 周期编号
     */
    private Long periodId;
    /**
     * 选择条件查询的周期描述
     */
    private String periodDesc;
    private Long dutyId;
    private Float startScore;//上升开始分值
    private Float endScore;  //上升结束分值

    private Long staId;//考核统计ID
    private String status;//周期分值上升或下降状态

    private boolean result;

    @Action("/exam/facetalk_main")
    @Override
    public String execute() throws Exception {
        listTitle = "考核面谈";
        listAction = "/exam/facetalk_list.xhtml";
        showChildOrg = true;
        organizations = organizationService.findChildren(null, true, null,
                null);
        if (organizations != null && organizations.size() > 0) {
        	if(getCurrentUserId() > 0){
            	Long topOrgId = getCurrentUser().getWork().getOrganization().getTopOrganization().getId();
            	Iterator<Organization> it = organizations.iterator();
            	while(it.hasNext()){
            		Organization o = it.next();
            		if(o.getId() != topOrgId){
            			it.remove();
            		}
            	}
            }
            organizationId = organizations.get(0).getId();
        }
        return "main";
    }

    @Action("/exam/facetalk_list")
    public String list() throws Exception {
        Calendar cal = Calendar.getInstance();
        // 统计结果
        if (periodDesc == null || periodDesc.equals("-1")
                || periodDesc.equals("0")) {
            // 当前周期
            cal.add(Calendar.MONTH, -1);
            periodId = examPeriodService.getExamPeriodByTypeAndYMonthly(
                    type, cal.get(Calendar.YEAR),
                    cal.get(Calendar.MONTH) + 1);
        } else if (periodDesc.split("-").length > 0) {
            periodId = examPeriodService.getExamPeriodByTypeAndYMonthly(
                    type, Integer.parseInt(periodDesc.split("-")[0]),
                    Integer.parseInt(periodDesc.split("-")[1]));
        } else {
            periodId = 0l;
        }
        examFacetalkPojos = examAppManageService.findAllEmpStatistics(type, periodId, organizationId, dutyId, startScore, endScore,status, createLimit(), createSorter());

        //分页
        this.paging(examFacetalkPojos);
        /* 设置翻页action */
        this.setPageUrl("exam/facetalk_list.xhtml");
        return "list";
    }

    @Action(value = "/ajax/modify_facetime_status", results = {
            @Result(name = "success", type = "json", params = {"root", "result"})
    })
    public String operator() throws Exception {
        if (staId > 0) {
            ExamStatistics examStatistics = examStatisticsService.getExamStatistics(staId);
            examStatistics.setFacetime(true);
            examStatisticsService.saveExamStatistics(examStatistics);
            result=true;
        }
        return SUCCESS;
    }

    public List<Organization> getOrganizations() {
        return organizations;
    }

    public void setOrganizations(List<Organization> organizations) {
        this.organizations = organizations;
    }

    public LimitedList<ExamFacetalkPojo> getExamFacetalkPojos() {
        return examFacetalkPojos;
    }

    public void setExamFacetalkPojos(LimitedList<ExamFacetalkPojo> examFacetalkPojos) {
        this.examFacetalkPojos = examFacetalkPojos;
    }

    public Long getDutyId() {
        return dutyId;
    }

    public void setDutyId(Long dutyId) {
        this.dutyId = dutyId;
    }

    public ExaminePeriodEnum getType() {
        return type;
    }

    public void setType(ExaminePeriodEnum type) {
        this.type = type;
    }

    public String getPeriodDesc() {
        return periodDesc;
    }

    public void setPeriodDesc(String periodDesc) {
        this.periodDesc = periodDesc;
    }

    public Long getPeriodId() {
        return periodId;
    }

    public void setPeriodId(Long periodId) {
        this.periodId = periodId;
    }

    public Float getStartScore() {
        return startScore;
    }

    public void setStartScore(Float startScore) {
        this.startScore = startScore;
    }

    public Float getEndScore() {
        return endScore;
    }

    public void setEndScore(Float endScore) {
        this.endScore = endScore;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public boolean isResult() {
        return result;
    }

    public void setResult(boolean result) {
        this.result = result;
    }

    public Long getStaId() {
        return staId;
    }

    public void setStaId(Long staId) {
        this.staId = staId;
    }
}
